<?php

class UserCredit extends AppModel{
    
    var $name='UserCredit';
    
    function get_user_ranking($user_id) {

        $get_credits = mysql_query("SELECT SUM(`credit`) as points ,`user_id`FROM `user_credits` GROUP BY `user_id` ORDER BY points DESC");
        $users = array();
        $rank = array();
        $i = '0';
        while ($row = mysql_fetch_assoc($get_credits)) {
            $users[$i]['id'] = $row['user_id'];
            $users[$i]['points'] = $row['points'];
            $i++;
        }
        //debug($users);

        $uids = Set::classicExtract($users, '{n}.id');

        $position = array_search($user_id, $uids);
        if ($position) {
            $rank['rank'] = $position + 1;
            $rank['points'] = $users[$position]['points'];
        } else {
            $rank['rank'] = count($uids) + 1;
        }


        return $rank;
    }

    function get_points($user_id){
        if($user_id){
            $data=mysql_query("SELECT SUM(credit) as points from `user_credits` where `user_id`='$user_id'");
            $points=mysql_fetch_array($data);
            if($data){
            $points=$points['points'];
            }else { $points=0;}
        }else{
            $points=0;
        }
        return $points ;
    }
    
    function get_level($points)
    {
        if(!is_numeric($points))
        {
            $points = 0;
        }

        if($points<999){
            $level="Cosmic Dust";
        } else if($points>999 && $points<2999){
            $level="Planet";
        } else if($points>2999 && $points<4999){
            $level="Star";
        } else if($points>4999 && $points<9999){
            $level="Galaxy";
        } else if($points>9999 && $points<100000){
            $level="Black Hole";
        } else if($points>100000 && $points<1000000){
            $level="Supernova";
        } else if($points>1000000){
            $level="Quantum";
        }
        return $level;
    }
    
}
?>
